/**
 * functions with pointer to structure arguments
 *
 * fireway
 * 2019年 09月 20日 星期五 07:23:29 CST
 */
#include <iostream>
#include <cmath>

// structure templates
struct Polar
{
    double distance;      // distance from origin
    double angle;         // direction from origin
};

struct Rect
{
    double x;             // horizontal distance from origin
    double y;             // vertical distance from origin
};

// prototypes
void rect_to_polar(const Rect * pxy, Polar * pda);
void show_polar (const Polar * pda);

int main()
{
    using namespace std;
    Rect rplace;
    Polar pplace;

    cout << "Enter the x and y values: ";
    while (cin >> rplace.x >> rplace.y)
    {
        rect_to_polar(&rplace, &pplace);    // pass addresses
        show_polar(&pplace);        // pass address
        cout << "Next two numbers (q to quit): ";
    }
    cout << "Done.\n";
    return 0;
}

// show Polar coordinates, converting angle to degrees
void show_polar (const Polar * pda)
{
    using namespace std;
    const double Rad_to_deg = 57.29577951;

    cout << "distance = " << pda->distance;
    cout << ", angle = " << pda->angle * Rad_to_deg;
    cout << " degrees\n";
}

// convert rectangular to Polar coordinates
// 第一个指针指向要转换的结构，第二个指针指向存储转换结果的结构体。
// 函数不返回一个新的结构体，而是修改调用函数中已有的结构体。
// 因此，虽然第一个参数是const指针，但第二个参数却不是。
void rect_to_polar(const Rect * pxy, Polar * pda)
{
    using namespace std;
    pda->distance =
        sqrt(pxy->x * pxy->x + pxy->y * pxy->y);
    pda->angle = atan2(pxy->y, pxy->x);
}

